/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.api.adwords.starterkit.utils;

import com.google.api.adwords.v201101.cm.ApiError;
import com.google.api.adwords.v201101.cm.ApiException;

import org.apache.log4j.Logger;
import org.json.JSONException;

import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.sql.SQLException;

import javax.xml.rpc.ServiceException;
import javax.xml.stream.XMLStreamException;

/**
 * The class <code>DumpExceptions</code> deals with error messages and
 * exceptions.
 *
 * @author rodrigo.f.vale@gmail.com (Rodrigo Vale)
 */
public class DumpExceptions {
  private static final Logger log = Logger.getLogger("StarterKit");

  /**
   * Dumps all exceptions from <code>IOException</code> to the log.
   *
   * @param e exception to dump
   */
  public static void dumpIOException(IOException e) {
    log.debug("IOException: " + e.getMessage());
    for (int i = 0; i < e.getStackTrace().length; i++) {
      log.debug(e.getStackTrace()[i]);
    }
  }
    
  /**
   * Dumps all exceptions from <code>JSONException</code> to the log.
   *
   * @param e exception to dump
   */
  public static void dumpJSONException(JSONException e) {
    log.debug("JSONException: " + e.getMessage());
    for (int i = 0; i < e.getStackTrace().length; i++) {
      log.debug(e.getStackTrace()[i]);
    }
  }

  /**
   * Dumps all exceptions from <code>ApiException</code> to the log.
   *
   * @param e exception to dump
   */
  public static void dumpApiException(ApiException e) {
    log.debug("ApiException: " + e.getMessage1());
    log.debug("  ErrorCode: " + e.getFaultCode());
    log.debug("  Trigger: " + e.getCause());
    log.debug("  Error: " + dumpArr(e.getErrors()));
  }

  /**
   * Dumps all exceptions from <code>ApiException</code> to the log.
   *
   * @param e exception to dump
   */
  public static void dumpApiException(
      com.google.api.adwords.v13.ApiException e) {
    log.debug("ApiException: " + e.getMessage1());
    log.debug("  ErrorCode: " + e.getFaultCode());
    log.debug("  Trigger: " + e.getCause());
    log.debug("  Trigger: " + e.toString());
    log.debug("  Trigger: " + e.dumpToString());
    log.debug("  Error: " + dumpArr(e.getErrors()));
  }
  
  /**
   * Dumps all exceptions from <code>ServiceException</code> to the log.
   *
   * @param e exception to dump
   */
  public static void dumpServiceException(ServiceException e) {
    log.debug("ServiceException: " + e.getMessage());
    for (int n = 0; n < e.getStackTrace().length; n++) {
      log.debug(e.getStackTrace()[n]);
    }
  }

  /**
   * Dumps all exceptions from <code>RemoteException</code> to the log.
   *
   * @param e exception to dump
   */
  public static void dumpRemoteException(RemoteException e) {
    log.debug("RemoteException: " + e.getMessage());
    for (int n = 0; n < e.getStackTrace().length; n++) {
      log.debug(e.getStackTrace()[n]);
    }
  }

  /**
   * Dumps all exceptions from <code>MalformedURLException</code> to the log.
   *
   * @param e exception to dump
   */
  public static void dumpMalformedURLException(MalformedURLException e) {
    log.debug("MalformedURLException: " + e.getMessage());
    for (int n = 0; n < e.getStackTrace().length; n++) {
      log.debug(e.getStackTrace()[n]);
    }
  }

  /**
   * Dumps all exceptions from <code>Exception</code> to the log.
   *
   * @param e exception to dump
   */
  public static void dumpException(Exception e) {
    log.debug("Exception: " + e.getMessage());
    for (int n = 0; n < e.getStackTrace().length; n++) {
      log.debug(e.getStackTrace()[n]);
    }
  }

  /**
   * Dumps a list of <code>ApiError</code>s to the log.
   *
   * @param errors the errors to dump
   */
  private static String dumpArr(ApiError[] errors) {
    if (errors != null) {
      if (errors.length > 0) {
        return dumpErr(errors[0]);
      }
    }
    return null;
  }

  /**
   * Dumps a list of <code>ApiError</code>s to the log.
   *
   * @param errors the errors to dump
   */
  private static String dumpArr(com.google.api.adwords.v13.ApiError[] errors) {
    if (errors != null) {
      if (errors.length > 0) {
        return dumpErr(errors[0]);
      }
    }
    return null;
  }

  /**
   * Dumps each <code>ApiError</code> to the log
   *
   * @param error an error to dump
   */
  private static String dumpErr(com.google.api.adwords.v13.ApiError error) {
    return "  ErrorType: " + error.getCode() + "\n"
        + "  FieldPath: " + error.getField() + "\n"
        + "  Trigger: " + error.getTrigger() + "\n";
  }

  /**
   * Dumps each <code>ApiError</code> to the log
   *
   * @param error an error to dump
   */
  private static String dumpErr(ApiError error) {
    return "  ErrorType: " + error.getApiErrorType() + "\n"
        + "  FieldPath: " + error.getFieldPath() + "\n"
        + "  Trigger: " + error.getTrigger() + "\n";
  }
  
  /**
   * Dumps each <code>SQLException</code> to the log
   *
   * @param e the SQLException exception to dump
   */
  public static void dumpSQLException(SQLException e) {
    log.debug("Exception: " + e.getMessage());
    for (int n = 0; n < e.getStackTrace().length; n++) {
      log.debug(e.getStackTrace()[n]);
    }
  }
  
  /**
   * Dumps all exceptions from <code>InterruptedException</code> to the log.
   *
   * @param e exception to dump
   */
  public static void dumpInterruptedException(InterruptedException e) {
    log.debug("IOException: " + e.getMessage());
    for (int i = 0; i < e.getStackTrace().length; i++) {
      log.debug(e.getStackTrace()[i]);
    }
  }

  /**
   * Dumps all exceptions from <code>XMLStreamException</code> to the log.
   *
   * @param e exception to dump
   */
  public static void dumpXMLStreamException(XMLStreamException e) {
    log.debug("IOException: " + e.getMessage());
    for (int i = 0; i < e.getStackTrace().length; i++) {
      log.debug(e.getStackTrace()[i]);
    }
  }
}
